import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import logger from 'src/common/utils/log';
import { WsEmitMsg } from 'src/common/constant/constant';
import { WebsocketsServer } from 'src/common/websocket.server';
import { MatchService } from './match.service';

@Injectable()
export class MatchListener {
  constructor(
    private readonly websocketServer: WebsocketsServer,
    private readonly matchService: MatchService,
  ) {}
  @OnEvent('match.playMatchUpdate')
  async pushPlayingMatchUpdate(tournamentId) {
    logger.info(`start pushPlayingMatchUpdate ${tournamentId}`)
    try {
      const playingMatches =
        await this.matchService.getInProgressMatches(tournamentId);
      this.websocketServer.server.emit(
        WsEmitMsg.UPDATE_PLAYING_MATCH,
        playingMatches,
      );
    } catch (e) {
      logger.error('pushPlayingMatchUpdate error', e);
      throw e;
    }
  }
}
